(function (global, $) {
    var baseUrl = 'http://259w758b32.zicp.vip/';
    var serial = '10000';

    function rest(type, url, data, callback) {
        if (typeof data === "function") {
            callback = data;
            data = {};
        }

        var der = $.Deferred();

        $.ajax({
            type: type,
            url: `${baseUrl}${url}?sbh=${serial}&timestamp=${Date.parse(new Date())}`,
            data: data,
            timeout: 10000,
            success: function (r) {
                if (r.code === 1000) {
                    if (typeof callback === 'function') {
                        callback.call(this, r.msg, r.data);
                    }
                    der.resolveWith(this, [r.msg, r.data]);
                } else {
                    der.rejectWith(this, [r.msg]);
                }
            },
            error: function (r) {
                switch (r.status) {
                    case 0:
                        der.rejectWith(this, ['请求超时']);
                        break
                    default:
                        der.rejectWith(this, ['未知错误']);
                }
            }
        });

        return der.promise();
    }

    $.extend({
        restGet: function () {
            return rest.apply(this, ['GET'].concat(Array.prototype.slice.call(arguments, 0)));
        },
        restPost: function () {
            return rest.apply(this, ['POST'].concat(Array.prototype.slice.call(arguments, 0)));
        },
        restBaseUrl: function () {
            return baseUrl;
        },
        restBasicUrl: function () {
            return baseUrl.replace("WebService/", "");
        }
    });
})(this, jQuery);
